package com.zh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.zh.common.ErrorCode;
import com.zh.config.RedisKeyConfig;
import com.zh.constant.UserConstant;
import com.zh.exception.ThrowUtils;
import com.zh.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "AuthController",value = "认证控制器")
@RestController
@RequestMapping("/auth")
@Slf4j
public class AuthController {

    private final RedisUtil redisUtil;

    public AuthController(RedisUtil redisUtil) {
        this.redisUtil = redisUtil;
    }


    @GetMapping("/publicKey")
    @ApiOperation("获取公钥")
    public String getPublicKey() {
        String publicKeyStr = redisUtil.get(RedisKeyConfig.PUBLICKEY);
        ThrowUtils.throwIf(StrUtil.isBlank(publicKeyStr), ErrorCode.SYSTEM_ERROR, "公钥获取失败");
        return publicKeyStr;
    }

    @GetMapping("/health")
    @ApiOperation("健康检查")
    public boolean checkHealthy() {
        return true;
    }

    @GetMapping("/check")
    @ApiOperation("检查登录态,轮询或者WebSocket")
    public boolean checkLogin() {
        return StpUtil.isLogin();
    }

    @GetMapping("/kick")
    @ApiOperation("踢出用户")
    @SaCheckLogin
    @SaCheckRole(value = {UserConstant.ADMIN, UserConstant.SUPER_ADMIN}, mode = SaMode.OR)
    private boolean kickOut(Integer id) {
        StpUtil.kickout(id);
        return true;
    }
}
